//
//  GoodsInStorageController.m
//  ios-supplier
//
//  Created by Benlai on 15/8/26.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "GoodsInStorageController.h"
#import "GoodsInStorageCell.h"
#import "JFBConfirmInStorage.h"
#import "JFBOrderProductDS.h"

@interface GoodsInStorageController () <GoodsInStorageCellDelegate>
@property (nonatomic, strong) UITableView *goodsTableView;
@end

@implementation GoodsInStorageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.goodsTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style: UITableViewStyleGrouped];
    self.goodsTableView.dataSource = self;
    self.goodsTableView.delegate = self;
    self.goodsTableView.backgroundColor = [UIColor colorFromHexValue: 0xeeeeee];
    [self.goodsTableView setSeparatorStyle: UITableViewCellSeparatorStyleNone];

    [self.goodsTableView registerNib: [UINib nibWithNibName: @"GoodsInStorageCell"
                                                     bundle: nil]
              forCellReuseIdentifier: @"goodsCell"];
    
    [self.view addSubview: self.goodsTableView];
    
    UIButton *confirmStorage = [UIButton buttonWithType: UIButtonTypeCustom];
    confirmStorage.frame = CGRectMake(0 , self.view.frame.size.height - 49, self.view.frame.size.width, 49);
    [confirmStorage setTitle: @"确认入库" forState: UIControlStateNormal];
    [confirmStorage setBackgroundColor: [UIColor colorFromHexValue: 0xf15353]];
    [confirmStorage addTarget: self action: @selector(confirmStorageBtnPressed) forControlEvents: UIControlEventTouchUpInside];
    
    [self.view addSubview: confirmStorage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodsData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 40;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        UIView *bgView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 40)];
        UILabel *goodsList = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, 200, 40)];
        goodsList.text = @"进货清单";
        goodsList.textColor = [UIColor colorFromHexValue: 0x676767];
        
        UILabel *inStorageCount = [[UILabel alloc] initWithFrame: CGRectMake(self.view.frame.size.width - 210, 0, 200, 40)];
        inStorageCount.text = @"入库数量";
        inStorageCount.textAlignment = NSTextAlignmentRight;
        inStorageCount.textColor = [UIColor colorFromHexValue: 0x676767];
        
        [bgView addSubview: goodsList];
        [bgView addSubview: inStorageCount];
        
        return bgView;
    }
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"goodsCell";
    GoodsInStorageCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier];
    cell.delegate = self;

    [cell setInfo: self.goodsData[indexPath.row]];
    
    [cell setSelectionStyle: UITableViewCellSelectionStyleNone];
    return cell;
}

-(void)confirmStorageBtnPressed
{
    [JFBUIAnimation alertSure: @"确认入库数量准确无误吗？" delegate: self];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        NSMutableArray *goodsArray = [NSMutableArray array];

        for (JFBOrderProductDS *goodsInfo in self.goodsData)
        {
            NSDictionary *goodsDic = @{@"product_id": [NSNumber numberWithInteger: goodsInfo.productId],
                                       @"quantity": [NSNumber numberWithInteger: goodsInfo.inStorage]};
            [goodsArray addObject: goodsDic];
        }

        [JFBConfirmInStorage confirmInStorage:^(BOOL isSuccess, NSError *error) {
            if (isSuccess)
            {
                [JFBUIAnimation alertTips: @"入库成功，库存已更新"];
                [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: 1] animated: YES];
            }
            else
            {
                 [JFBUIAnimation alertTips: @"入库失败，请稍后再试"];
            }
        } orderID: self.orderID storage: goodsArray];
    }
}

-(void)goodsInStorageChanged: (NSInteger)count
{
    
}
@end
